package com.apanal.qlife.common.util;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.push.model.PushMsg;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.ITemplate;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.base.payload.APNPayload.SimpleAlertMsg;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;

/**
 * 推送工具类
 * 
 * 
 * @author shuliangxing
 * 
 * @date 2015-7-31上午10:14:27
 */
public class PushUtil {

	/**
	 * 默认推送离线保留时间,一星期
	 */
	private static final long DEFAULT_OFF_LINE_TIME = 1000 * 3600 * 24 * 7;

	public static void main(String[] args) throws Exception {

		IGtPush push = getPush();
		// TransmissionTemplate template = TransmissionTemplateDemo();
		// LinkTemplate template = linkTemplateDemo();
		// NotificationTemplate template = notificationTemplateDemo();
		PushMsg pushMsg = new PushMsg(2, "紧急停水公告", 1, 1, 1);
		String json = JSON.toJSONString(pushMsg);
		System.out.println(json);
		// TransmissionTemplate template = getTransTemplate(json, "ios APN消息");
		// AppMessage message = getAppMsg(Constants.PUSH_APP_ID, template);
		// AppMessage message = new AppMessage();
		// message.setData(template);
		//
		// message.setOffline(true);
		// message.setOfflineExpireTime(24 * 1000 * 3600);
		//
		// List<String> appIdList = new ArrayList<String>();
		// List<String> phoneTypeList = new ArrayList<String>();
		// List<String> provinceList = new ArrayList<String>();
		// List<String> tagList = new ArrayList<String>();
		//
		// appIdList.add(Constants.PUSH_APP_ID);
		// // phoneTypeList.add("ANDROID");
		// // provinceList.add("");
		// // tagList.add("脾气");
		//
		// message.setAppIdList(appIdList);
		// message.setPhoneTypeList(phoneTypeList);
		// message.setProvinceList(provinceList);
		// message.setTagList(tagList);
		// message.setPushNetWorkType(1);
		// message.setSpeed(1000);
		// IPushResult ret = push.pushMessageToApp(message);

		System.out.println("====透传json=" + json);

		ITemplate template = PushUtil.getTransTemplate(json, "ios apn消息");
		// 根据app群推
		AppMessage message = PushUtil
				.getAppMsg(Constants.PUSH_APP_ID, template);
		IPushResult ret = push.pushMessageToApp(message);

		// 根据别名群推
		// ListMessage message = PushUtil.getListMsg(template);
		// // 推送任务id,即taskId
		// String contentId = push.getContentId(message);
		// System.out.println("==contentId=" + contentId);
		//
		// List<String> aliasList = new ArrayList<String>();
		// aliasList.add("114");
		// aliasList.add("115");
		// List<Target> targetList = PushUtil.getTargetList(aliasList);
		//
		// IPushResult ret = push.pushMessageToList(contentId, targetList);

		System.out.println("getMessageId=" + ret.getMessageId());
		System.out.println("getTaskId=" + ret.getTaskId());
		System.out.println("getResultCode=" + ret.getResultCode());
		System.out.println("getResponse=" + ret.getResponse().toString());
	}

	/**
	 * 获取个推push实例
	 * 
	 * @return
	 */
	public static IGtPush getPush() {
		return new IGtPush(Constants.PUSH_HOST, Constants.PUSH_APP_KEY,
				Constants.PUSH_MASTER_SECRET);
	}

	/**
	 * 获得指定应用群推的消息对象
	 * 
	 * @param appId
	 *            应用id
	 * @param template
	 *            模板
	 * @param offLineTime
	 *            离线保留时间
	 * @return
	 */
	public static AppMessage getAppMsg(String appId, ITemplate template,
			long offLineTime) {
		AppMessage message = new AppMessage();
		// 推送消息消息内容
		message.setData(template);
		// 消息离线是否存储
		message.setOffline(true);
		// 消息离线存储多久，单位为毫秒
		message.setOfflineExpireTime(offLineTime);
		// 是否wifi推送，1：wifi推送；0：不限制推送方式
		message.setPushNetWorkType(0);

		List<String> appIdList = new ArrayList<String>();

		// List<String> phoneTypeList = new ArrayList<String>();
		// List<String> provinceList = new ArrayList<String>();
		// List<String> tagList = new ArrayList<String>();

		appIdList.add(appId);
		// phoneTypeList.add("ANDROID");//IOS
		// provinceList.add("深圳");
		// tagList.add("vip");

		message.setAppIdList(appIdList);
		return message;
	}

	/**
	 * 获得指定应用群推的消息对象
	 * 
	 * @param appId
	 *            应用id
	 * @param template
	 *            模板
	 * @return
	 */
	public static AppMessage getAppMsg(String appId, ITemplate template) {
		return getAppMsg(appId, template, DEFAULT_OFF_LINE_TIME);
	}

	/**
	 * 获得指定列表用户群推的消息对象
	 * 
	 * @param template
	 *            模板
	 * @param offLineTime
	 *            离线保留时间
	 * @return
	 */
	public static ListMessage getListMsg(ITemplate template, long offLineTime) {
		// 根据别名群推
		ListMessage message = new ListMessage();
		message.setData(template);
		// 一星期
		message.setOfflineExpireTime(offLineTime);

		return message;
	}

	/**
	 * 获得指定列表用户群推的消息对象
	 * 
	 * @param template
	 *            模板
	 * @return
	 */
	public static ListMessage getListMsg(ITemplate template) {
		return getListMsg(template, DEFAULT_OFF_LINE_TIME);
	}

	/**
	 * 获得透传(自定义)模板
	 * 
	 * @param content
	 *            透传内容，不支持转义字符
	 * @param iosAlertMsg
	 *            ios ANP通知消息内容
	 * @return
	 */
	public static TransmissionTemplate getTransTemplate(String content,
			String iosAlertMsg) {
		TransmissionTemplate template = new TransmissionTemplate();
		template.setAppId(Constants.PUSH_APP_ID);
		template.setAppkey(Constants.PUSH_APP_KEY);
		// 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
		template.setTransmissionType(2);
		template.setTransmissionContent(content);

		// ios模板
		APNPayload apnPayload = new APNPayload();
		SimpleAlertMsg alertMsg = new SimpleAlertMsg(iosAlertMsg);
		apnPayload.setAlertMsg(alertMsg);
		// 通知铃声文件名
		apnPayload.setSound("default");
		// 应用icon上显示的数字
		apnPayload.setBadge(1);
		// 推送直接带有透传数据
		apnPayload.setContentAvailable(1);
		// 在客户端通知栏触发特定的action和button显示
		// apnPayload.setCategory("ACTIONABLE");
		// 字典模式使用下者
		// apnPayload.setAlertMsg(getDictionaryAlertMsg());
		template.setAPNInfo(apnPayload);
		return template;
	}

	/**
	 * 根据别名集合获得推送目标对象集合
	 * 
	 * @param aliasList
	 * @return
	 */
	public static List<Target> getTargetList(List<String> aliasList) {
		List<Target> targetList = new ArrayList<Target>();
		for (String alias : aliasList) {
			Target target = new Target();
			target.setAppId(Constants.PUSH_APP_ID);
			// 用户别名推送，clientId和用户别名两者只能选其一
			target.setAlias(alias);
			targetList.add(target);
		}
		return targetList;
	}

}
